﻿Imports DAO
Imports DTO
Imports System.Resources
Imports System.Globalization
Imports System.Reflection

Public Class SachBUS

    ' load Thông Tin Sách Lên DataGridView
    Public Function LaySach() As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LayDanhSachSach()
        Return dt
    End Function

    Public Sub NhapSach(ByVal sach As SachDTO, ByVal ct As ChiTietSachDTO)
        Dim kt As New QuyDinhDAO
        Dim dt As New DataTable
        dt = kt.LayQuyDinh()
        Dim dr As DataRow = dt.Rows(0)
        Dim nhapmin As Integer = Integer.Parse(dr.Item("SoLuongSachNhapMin"))

        If sach.LaySoLuong < nhapmin Then
            Throw New Exception("Số lượng nhập phải lớn hơn quy định")
        End If
        '     Dim sl As Integer = SoLuong(sach)
        '   sach.LaySoLuong += sl
        Dim sach1 As New SachDAO
        sach1.NhapSach(sach, ct)

    End Sub

    Public Sub NhapSachTonTai(ByVal sach As SachDTO, ByVal ct As ChiTietSachDTO)
        Dim kt As New QuyDinhDAO
        Dim dt As New DataTable
        dt = kt.LayQuyDinh()
        Dim dr As DataRow = dt.Rows(0)
        Dim nhapmin As Integer = Integer.Parse(dr.Item("SoLuongSachNhapMin"))

        Dim tonmin As Integer = Integer.Parse(dr.Item("SoLuongTonMin"))

        Dim KTTonMin As Integer = SoLuong(sach) 'KiemTraSoLuong()

        If KTTonMin > tonmin Then
            Throw New Exception("Số lượng đầu Sách vẫn còn nhiều không cần nhập thêm")
        End If

        If sach.LaySoLuong < nhapmin Then
            Throw New Exception("Số lượng nhập phải lớn hơn quy định")
        End If
        Dim sl As Integer = SoLuong(sach)
        sach.LaySoLuong += sl
        Dim sach1 As New SachDAO
        sach1.NhapSachTonTai(sach, ct)

    End Sub

    Public Sub XoaSach(ByVal sach As SachDTO)

        Dim s As New SachDAO()
        s.XoaSach(sach)

    End Sub

    Public Sub CapNhat(ByVal sach As SachDTO)

        Dim s As New SachDAO
        s.CapNhatSach(sach)
    End Sub

    Public Function KiemTraSoLuong() As Integer
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.KiemTaSoLuong()
        Dim sl As Integer = 0
        Dim dr As DataRow = dt.Rows(0)
        sl = dr.Item("SoLuong")
        Return sl
    End Function

    Public Function SoLuong(ByVal id As SachDTO) As Integer
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.laySoLuong(id)
        Dim sl As Integer = 0
        Dim dr As DataRow = dt.Rows(0)
        sl = dr.Item("SoLuong")
        Return sl
    End Function

    Public Function LayGia(ByVal id As SachDTO) As Integer
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LayGia(id)
        Dim sl As Integer = 0
        Dim dr As DataRow = dt.Rows(0)
        sl = dr.Item("Gia")
        Return sl
    End Function

    Public Sub XoaTheloai(ByVal idtheloai As TheLoaiDTO)
        Dim theloai As New TheLoaiDAO
        theloai.XoaTheLoai(idtheloai)
    End Sub

    Public Function LayTenSach(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LayTenSach(s)
        Return dt
    End Function

    ' Public Function ThongKeT()

    Public Function Tim(ByVal s As String) As DataTable
        Dim dt As New DataTable
        Dim ti As New SachDAO
        dt = ti.tim(s)
        Return dt
    End Function

    Public Function TimTheoTen(ByVal s As String) As DataTable
        Dim dt As New DataTable
        Dim ten As New SachDAO
        dt = ten.timtheoten(s)
        If dt.Rows.Count = 0 Then
            Throw New Exception("Không tìm thấy")
        End If
        Return dt
    End Function

    Public Function TimTheoTacGia_TheLoai(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LaySachTheoTheLoaiVaTacGia(s)
        Return dt
    End Function

    Public Function TimTheoTacGia(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.timtheotacgia(s)
        Return dt
    End Function


    Public Function TimTheoTheLoai(ByVal s As SachDTO) As DataTable
        Dim dt As New DataTable
        Dim sach As New SachDAO
        dt = sach.LaySachTheoTheLoai(s)
        Return dt
    End Function
End Class
